package dryven.model.binding.fieldset;

import java.lang.reflect.AnnotatedElement;

import dryven.model.validation.Validator;
import dryven.view.DefaultViewAnnotationComponent;
import dryven.view.Formatter;

public abstract class AnnotatedField extends FieldBase {
	
	private Access _access;
	private Class<? extends Validator> _validatorClass;
	private Class<? extends Formatter> _formatterClass;
	private String _viewComponent;
	private boolean _required;
	
	public AnnotatedField(AnnotatedElement annotations) {
		dryven.annotations.Validator va = annotations.getAnnotation(dryven.annotations.Validator.class);
		if(va!=null) {
			_validatorClass = va.value();
			_required = va.required();
		}
		dryven.annotations.Formatter fa = annotations.getAnnotation(dryven.annotations.Formatter.class);
		if(fa!=null) {
			_formatterClass = fa.value();
		}
		dryven.annotations.View viewa = annotations.getAnnotation(dryven.annotations.View.class);
		if(viewa!=null) {
			if(!viewa.component().equals(DefaultViewAnnotationComponent.class)) {
				_viewComponent = viewa.component().getName();
			}
			else if(!viewa.componentString().equals("")) {
				_viewComponent = viewa.componentString();
			} else {
				_viewComponent = "default";
			}
			_access = viewa.access();
		}
	}
	
	public AnnotatedField overrideValidator(Class<? extends Validator> validatorClass) {
		_validatorClass = validatorClass;
		return this;
	}
	
	public AnnotatedField overrideViewComponent(String name) {
		_viewComponent = name;
		return this;
	}
	
	public AnnotatedField overrideFormatter(Class<? extends Formatter> formatterClass) {
		_formatterClass = formatterClass;
		return this;
	}
	
	public AnnotatedField overrideAccess(Access a) {
		_access = a;
		return this;
	}
	
	public AnnotatedField overrideRequired(boolean required) {
		_required = required;
		return this;
	}
	
	@Override
	public Access getAccess() {
		return _access;
	}

	@Override
	public Class<? extends Validator> getValidatorClass() {
		return _validatorClass;
	}

	@Override
	public String getViewComponentClass() {
		return _viewComponent;
	}

	@Override
	public Class<? extends Formatter> getFormatterClass() {
		return _formatterClass;
	}
	
	@Override
	public boolean isRequired() {
		return _required;
	}
}
